<!DOCTYPE html>
<html>
<head>
    <title>Circuit Diagram 4.33.1</title>
    <style>
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            flex-direction: column;
            margin-top: 20px;
        }
        canvas {
            border: 1px solid #d3d3d3;
        }
    </style>
</head>
<body>
    <canvas id="circuitCanvas" width="600" height="550"></canvas>
    <script>
        const canvas = document.getElementById('circuitCanvas');
        const ctx = canvas.getContext('2d');
        ctx.font = '20px Arial';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';

        function drawResistor(x, y, w, h, label) {
            ctx.beginPath();
            ctx.rect(x, y, w, h);
            ctx.stroke();
            ctx.fillText(label, x + w + 15, y + h / 2);
        }

        function drawCapacitor(x, y, h, gap, label, labelPos) {
            ctx.beginPath();
            ctx.moveTo(x - gap / 2, y - h / 2);
            ctx.lineTo(x - gap / 2, y + h / 2);
            ctx.stroke();

            ctx.beginPath();
            ctx.moveTo(x + gap / 2, y - h / 2);
            ctx.lineTo(x + gap / 2, y + h / 2);
            ctx.stroke();
            
            if (labelPos === 'above') {
                ctx.fillText(label, x, y - h / 2 - 15);
            } else {
                 ctx.fillText(label, x, y + h / 2 + 15);
            }
        }

        function drawSwitch(x1, y1, angle, len, label, labelPos) {
            const x2 = x1 + len * Math.cos(angle * Math.PI / 180);
            const y2 = y1 + len * Math.sin(angle * Math.PI / 180);
            
            // Fixed point
            ctx.beginPath();
            ctx.arc(x1, y1, 3, 0, 2 * Math.PI);
            ctx.fill();

            // Arm
            ctx.beginPath();
            ctx.moveTo(x1, y1);
            ctx.lineTo(x2, y2);
            ctx.stroke();

            // Label
            if (labelPos === 'right') ctx.fillText(label, x1 + 30, y1);
            if (labelPos === 'left') ctx.fillText(label, x1 - 25, y1);
            if (labelPos === 'below') ctx.fillText(label, x1, y1 + 30);
            if (labelPos === 'above') ctx.fillText(label, x1, y1 - 25);
        }

        function drawBattery(x, y, label) {
            // Positive plate (long)
            ctx.beginPath();
            ctx.moveTo(x, y - 15);
            ctx.lineTo(x, y + 15);
            ctx.stroke();

            // Negative plate (short)
            ctx.beginPath();
            ctx.moveTo(x + 20, y - 8);
            ctx.lineTo(x + 20, y + 8);
            ctx.stroke();
            
            ctx.fillText(label, x + 10, y + 35);
        }

        // --- Main Drawing Logic ---
        
        // Define coordinates
        const y_top = 100;
        const y_cap = 200;
        const y_bottom = 350;
        const y_batt = 450;

        const x_L_main = 100;
        const x_C1 = 150;
        const x_R = 100;
        const x_k3 = 225;
        const x_C2 = 300;
        const x_k2 = 375;
        const x_C3 = 450;
        const x_R_main = 525;
        
        ctx.lineWidth = 1.5;

        // --- Wires ---
        ctx.beginPath();
        // Top rail
        ctx.moveTo(x_R, y_top);
        ctx.lineTo(x_k3, y_top);
        
        // k4 section
        ctx.moveTo(x_k3 - 50, y_top);
        ctx.lineTo(x_R, y_top);

        // Right vertical wire
        ctx.moveTo(x_R_main, y_top);
        ctx.lineTo(x_R_main, y_batt);

        // Capacitor rail
        ctx.moveTo(x_L_main, y_cap);
        ctx.lineTo(x_C1 - 10, y_cap);
        ctx.moveTo(x_C1 + 10, y_cap);
        ctx.lineTo(x_C2 - 10, y_cap);
        ctx.moveTo(x_C2 + 10, y_cap);
        ctx.lineTo(x_C3 - 10, y_cap);
        ctx.moveTo(x_C3 + 10, y_cap);
        ctx.lineTo(x_R_main, y_cap);

        // Left vertical wire (with R)
        ctx.moveTo(x_L_main, y_top);
        ctx.lineTo(x_L_main, y_cap - 50);
        ctx.moveTo(x_L_main, y_cap - 20);
        ctx.lineTo(x_L_main, y_cap);
        
        // Switch k3 wire
        ctx.moveTo(x_k3, y_top);
        ctx.lineTo(x_k3, y_cap);
        
        // Switch k2 wire
        ctx.moveTo(x_k2, y_cap);
        ctx.lineTo(x_k2, y_bottom);
        
        // Bottom rail
        ctx.moveTo(x_L_main, y_bottom);
        ctx.lineTo(x_k2, y_bottom);
        
        // Battery connections
        ctx.moveTo(x_L_main, y_bottom);
        ctx.lineTo(x_L_main, y_batt);
        ctx.moveTo(x_L_main + 20, y_batt); // Positive terminal
        ctx.lineTo(250, y_batt);
        
        ctx.moveTo(x_R_main-20, y_batt); // Negative terminal
        ctx.lineTo(250+40, y_batt);
        
        ctx.stroke();

        // --- Components ---
        
        // Resistor R
        drawResistor(x_R - 5, y_cap - 50, 10, 30, 'R');
        
        // Capacitors
        drawCapacitor(x_C1, y_cap, 30, 20, 'C₁', 'above');
        drawCapacitor(x_C2, y_cap, 30, 20, 'C₂', 'above');
        drawCapacitor(x_C3, y_cap, 30, 20, 'C₃', 'above');
        
        // Switches
        drawSwitch(x_R_main, 300, 225, 30, 'k₁', 'left');
        drawSwitch(x_k2, y_bottom, 225, 30, 'k₂', 'left');
        drawSwitch(x_k3, y_cap - 50, 45, 30, 'k₃', 'right');
        drawSwitch(x_L_main, y_top, 315, 30, 'k₄', 'below');
        
        // Battery
        drawBattery(250, y_batt, 'ε');

        // Diagram Label
        ctx.font = '24px "KaiTi", "SimSun", serif';
        ctx.fillText('电图 4.33.1', 300, 520);
    </script>
</body>
</html>